package com.swt.gap.scheduled;

import com.swt.gap.common.constant.RedisConstant;
import com.swt.gap.common.graph.Graph;
import com.swt.gap.common.graph.GraphProcessor;
import com.swt.gap.util.RedisUtil;

import java.util.Set;

/**
 * 将一个任务的图数据保存至redis
 *
 * @author sx
 * @date 2020/7/29 15:09
 */
public class RedisTaskGraphRunnable implements Runnable {

    private final RedisUtil redisUtil;
    private final Integer taskId;
    private final GraphProcessor graphProcessor;

    public RedisTaskGraphRunnable(RedisUtil redisUtil, Integer taskId, GraphProcessor graphProcessor) {
        this.redisUtil = redisUtil;
        this.taskId = taskId;
        this.graphProcessor = graphProcessor;
    }

    @Override
    public void run() {
        redisUtil.set(RedisConstant.KEY_PREFIX_TASK + taskId, RedisConstant.SAVE_PENDING);
        Set<Graph.Link> links = graphProcessor.listRelationshipsByTaskId(taskId);
        links.forEach(link -> {
            redisUtil.set(RedisConstant.KEY_PREFIX_RELATIONSHIP + taskId + "_" + link.getId(), link.getId());
        });
        redisUtil.set(RedisConstant.KEY_PREFIX_TASK + taskId, RedisConstant.SAVE_FINISHED);
    }
}
